<?php defined('SYSPATH') or die('No direct script access.');
/**
 * @file linkmeta.php
 * @brief linkmeta管理
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-17
 */
class Controller_Linkmeta extends Controller_Rest {
	public function before()
	{
		parent::before();
	}

	/**
	 * @brief 获取linkmeta数据
	 *
	 * @param $linkmeta_id
	 *
	 * @return $this
	 */
	public function action_get($linkmeta_id=NULL)
	{
		if($linkmeta_id) $this->get_linkmeta($linkmeta_id);
		else $this->get_linkmetas();
	}
	
	/**
	 * @brief 获取指定linkmeta_id的linkmeta数据
	 *
	 * @param $linkmeta_id
	 *
	 * @return $this 
	 */
	private function get_linkmeta($linkmeta_id)
	{
		$linkmeta = ORM::factory('linkmeta', $linkmeta_id);
		if(!$linkmeta->meta_id)
		{
			$this->response->body('The linkmeta not exist！')->status(206);
			return;
		}
		$res = json_encode($linkmeta->as_array());
		$this->response->body($res)->status(200);
	}
	
	/**
	 * @brief 获取指定起始位置的linkmeta信息
	 *
	 * @return $this 
	 */
	private function get_linkmetas()
	{
		$linkmeta = ORM::factory('linkmeta');
		/** 获取起止数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);

		/** 获取指定的link*/
		$link_id = filter_input(INPUT_GET, 'link_id');
		$start = ($start) ? $start : 0;
		$pos = ($pos) ? $pos : PHP_INT_MAX;

		if($link_id)
		{
			/** 获取指定link_id的linkmeta*/
			$linkmetas = $linkmeta
					->where('link_id', '=', $link_id)
					->offset($start)
					->limit($pos)
					->find_all();	
		}
		else
		{/** 获取指定范围的linkmeta*/
			$linkmetas = $linkmeta
					->offset($start)
					->limit($pos)
					->find_all();
		}

		if(!$linkmetas->count())
		{
			$this->response->body('no linkmeta exists!')->status(202);
		}
		else
		{
			$res['num'] = $linkmetas->count();
			$res['list'] = array();
			foreach($linkmetas as $lnt)
			{
				$res['list'][] = $lnt->as_array();
			}
			$json_res = json_encode($res);
			$this->response->body($json_res)->status(200);
		}
	}
	
	/**
	 * @brief 添加linkmeta
	 *
	 * @return $this
	 */
	public function action_post()
	{
		$linkmeta = ORM::factory('linkmeta');
		if(!$_POST)
		{
			$this->response->body('have no link data!')->status(206);
			return;
		}
		if(!$_POST['link_id'])
		{
			$this->response->body('have no link_id!')->status(202);
		}
		if(isset($_POST['meta_id']))
		{
			unset($_POST['meta_id']);
		}
		try
		{
			/** 匹配对应的link*/
			$link = ORM::factory('link', $_POST['link_id']);
			if(!$link->link_id)
			{
				$this->response->body('link no match!')->status(202);
				return;
			}
			$linkmeta->values($_POST)->check()->save();
			$this->response->body($_POST['meta_key'].' add success!')->status(200);
		}
		catch(ORM_Validation_Exception $e)
		{/** 验证错误 */
			$errors = $e->errors();
			foreach($errors as $k=>$v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
		catch(Exception $err)
		{/** 其他错误*/
			$this->response->body($err->getMessage())->status(500);
		}
	}
	
	/**
	 * @brief 更新linkmeta
	 *
	 * @param $linkmeta_id
	 *
	  * @return $this
	 */
	public function action_put($linkmeta_id)
	{
		$linkmeta = ORM::factory('linkmeta', $linkmeta_id);
		if(!$linkmeta->meta_id)
		{
			$this->response->body('The linkmeta not exists!')->status(206);
			return;
		}
		$new_link = $this->request->post();
		if(!$new_link)
		{
			$this->response->body('no linkmeta content post!')->status(206);
			return;
		}

		$link_id = $this->request->post('link_id');
		if(!$link_id)
		{
			$this->response->body('have no link_id!')->status(206);
			return;
		}
		try
		{
			/** 匹配对应的link*/
			$link = ORM::factory('link', $link_id);
			if(!$link->link_id)
			{
				$this->response->body('link not match!')->status(202);
				return;
			}
			$linkmeta->values($new_link)->check()->save();
			$this->response->body($this->request->post('meta_key').' add success!')->status(200);
		}
		catch(ORM_Validation_Exception $e)
		{
			$errors = $e->errors();
			foreach($errors as $k=>$v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
		catch(Exception $err)
		{
			$this->response->body($err->getMessage())->status(500);
		}
	}
	
	/**
	 * @brief 删除linkmeta
	 *
	 * @param $linkmeta_id
	 *
	 * @return $this
	 */
	public function action_delete($linkmeta_id)
	{
		$linkmeta = ORM::factory('linkmeta', $linkmeta_id);
		if(!$linkmeta->meta_id)
		{
			$this->response->body('The linkmeta not exists!')->status(206);
			return;
		}
		try
		{
			$linkmeta->delete();
			$this->response->body('"'.'linkmeta-'.$linkmeta_id.'" delete success!')->status(200);
		}
		catch(Exception $e)
		{
			$this->response->body($e->getMessage())->status(500);
		}
	}
}
